//
//  TouTiaoViewController.m
//  網易
//
//  Created by 赵天宇 on 16/8/22.
//  Copyright © 2016年 赵天宇. All rights reserved.
//

#import "TouTiaoViewController.h"
#import "NewTableViewCell.h"
#import "Data.h"
#import <MJRefresh.h>
#import <SDCycleScrollView.h>
#import "NextTouTiaoViewController.h"
#import "Reachability.h"
@interface TouTiaoViewController ()<UITableViewDataSource,UITableViewDelegate,SDCycleScrollViewDelegate>{
    UITableView *tableV;
    NSDictionary *dic;
    int a;
    NSString *httpArg;
}

@end
#define  TEST_URL  @"http://api.tianapi.com/apple/?key=158cc0978fcc0b6a04f8ccf60f56ff8d"
//#define TEST_URL @"http://apis.baidu.com/txapi/world/world?num=10&page=1";
@implementation TouTiaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    a=10;
    httpArg =[NSString stringWithFormat:@"num=10&page=%d",a];
    self.automaticallyAdjustsScrollViewInsets=YES;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receive:) name:@"hello" object:nil];
//     [[Data sharedData]getData:TEST_URL];
    [[Data sharedData]request:TEST_URL withHttpArg:httpArg];
//初始化表格
    tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    tableV.delegate=self;
    tableV.dataSource=self;
    tableV.mj_header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(header)];
        tableV.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footer)];
    [self.view addSubview:tableV];
}
-(void)receive:(NSNotification *)noT{
    dic=noT.object;
    [tableV reloadData];
}
-(void)header{
    a++;
    [[Data sharedData]request:TEST_URL withHttpArg:httpArg];

    [tableV.mj_header endRefreshing];
    [tableV reloadData];
}
-(void)footer{
    [tableV reloadData];
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSArray *aa  = [dic objectForKey:@"newslist"];
    NSMutableArray *bb=[NSMutableArray array];
    for (NSDictionary *dd in aa) {
        [bb addObject:[dd objectForKey:@"picUrl"]];
    }
    NSArray *imageArray=@[@"http://imgsrc.baidu.com/baike/pic/item/a0ca99d68f1ac76307088b97.jpg",@"http://epaper.xiancn.com/xarb/res/1/20100227/58731267215555820.jpg",@"http://www.xwzgw.com/pic/news/201512/14497349184_0.jpg",@"http://www.kf.cn/kfrb/rmp/1/1/2014-06/13/08/res01_attpic_brief.jpg"];
    SDCycleScrollView *cycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) delegate:self placeholderImage:[UIImage imageNamed:@"home_header_slogon@2x.png"]];
    cycleView.imageURLStringsGroup =imageArray;
    return cycleView;

}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
    return 200;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *dd  = [dic objectForKey:@"newslist"];
    return dd.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString*ss=@"love";
    NewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ss];
    if(!cell){
        cell= [[NewTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ss];
    }
    NSArray *aa  = [dic objectForKey:@"newslist"];
    NSDictionary *dic_2 =aa[indexPath.row];
    NSURL *url = [NSURL URLWithString:[dic_2 objectForKey:@"picUrl"]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    cell.imageV.image = [UIImage imageWithData:data scale:20];
      cell.imageV.image = [UIImage imageWithData:data];
    cell.fuLab.text= [dic_2 objectForKey:@"title"];
    cell.leftLab.text=[dic_2 objectForKey:@"ctime"];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *aa  = [dic objectForKey:@"newslist"];
    NSDictionary *dic_2 =aa[indexPath.row];
        NextTouTiaoViewController *tt= [[NextTouTiaoViewController alloc]init];
    NSString *string =[dic_2 objectForKey:@"url"];
        tt.ss=string;
        CATransition *tran = [CATransition animation];
        tran.duration=1.5;
        //    tran.type=@"cameraIrisHollowOpen";
        //    pageCurl            向上翻一页
        //    pageUnCurl          向下翻一页
        //    rippleEffect        滴水效果
        //    suckEffect          收缩效果，如一块布被抽走
        //    cube                立方体效果
        //    oglFlip             上下翻转效果
        tran.type=@"rippleEffect";
        tran.subtype=kCAAlignmentLeft;
        [self.view.window.layer addAnimation:tran forKey:nil];
        [self presentViewController:tt animated:YES completion:nil];
    
//    }else if (indexPath.row==1){
//        NextTouTiaoViewController *tt= [[NextTouTiaoViewController alloc]init];
//        NSString *string =@"ttp://c.m.163.com/news/a/C0I44QUL00051C89.html?spss=newsapp&spsw=1";
//        tt.ss=string;
//        CATransition *tran = [CATransition animation];
//        tran.duration=3;
//        //    tran.type=@"cameraIrisHollowOpen";
//        //    pageCurl            向上翻一页
//        //    pageUnCurl          向下翻一页
//        //    rippleEffect        滴水效果
//        //    suckEffect          收缩效果，如一块布被抽走
//        //    cube                立方体效果
//        //    oglFlip             上下翻转效果
//        tran.type=@"rippleEffect";
//        tran.subtype=kCAAlignmentLeft;
//
//        [self presentViewController:tt animated:YES completion:nil];
//
//    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
